var Rows = {
    fieldNames: null, // all field names of current table
    primaryFields: new Array, // the index of the fields on this.fieldNames that are primary keys
    oldValues: new Array,
    FindFields: function( table ) { // not in use
        var firstRow = table.getElementsByTagName( 'tr' )[ 0 ];
        var cells = firstRow.getElementsByTagName( 'th' );
        this.fieldNames = new Array();
        for ( i in cells ) {
            var cell = cells[ i ];
            if ( typeof cell.nodeType == 'undefined' ) {
                continue;
            }
            for ( j = 0; j < cell.childNodes.length; ++j ) {
                var child = cell.childNodes[ j ];
                if ( child.nodeType == 3 ) { // text node
                    var fieldName = child.nodeValue;
                    this.fieldNames[ this.fieldNames.length ] = fieldName;
                    if ( cell.className.indexOf( 'primary' ) != -1 ) {
                        this.primaryFields[ this.primaryFields.length ] = i;
                    }
                    break;
                }
            }
        }
    },
    GetRowValues: function( row ) {
        var values = new Array;
        for ( i in row.childNodes ) {
            var cell = row.childNodes[ i ];
            if ( typeof cell.nodeValue == 'undefined' ) { // not a <td>
                continue;
            }
            if ( cell.getElementsByTagName( 'input' ).length > 0 ) {
                values[ i ] = cell.getElementsByTagName( 'input' )[ 0 ].value;
            }
            else {
                values[ i ] = '';
				for ( j in cell.childNodes ) {
					if ( cell.childNodes[ j ].nodeType == 3 ) {
						values[ i ] = cell.childNodes[ j ].nodeValue;
						break;
					}
				}
            }
        }
        return values;
    },
	GoDelete: function( row ) {
		Modals.Confirm( 
            "Do you want to delete this row?",
            ( function( row ) { 
                return function() {
                    Rows.DoDelete( row );
                }
            } )( row )
        );
	},
    DoDelete: function( row ) {
        var curpage = Metadata.GetData()[ 2 ];
        var values = this.GetRowValues( row );
        var table = row.parentNode;
        var isempty = false;
        if ( table.getElementsByTagName( 'tr' ).length == 2 ) { // title and insert row only
            isempty = true;
        }
        Coala.Warm( 'table/row/delete', { "@values": values, "curpage": curpage, "isempty": isempty } );
    },
	GoEdit: function( which, server, db, table ) {
		row = which.parentNode.parentNode;
		rows = row.getElementsByTagName( 'td' );
        this.oldValues[ this.oldValues.length ] = new Array;
		for ( cell in rows ) {
			field = rows[ cell ];
			if ( typeof field.nodeType != 'undefined' ) {
				t = '';
				for ( i in field.childNodes ) {
					if ( field.childNodes[ i ].nodeType == 3 ) {
						t = field.childNodes[ i ].nodeValue;
						break;
					}
				}
                this.oldValues[ this.oldValues.length - 1 ].push( t );
				while ( field.firstChild ) {
					field.removeChild( field.firstChild );
				}
				input = document.createElement( 'input' );
				input.type = 'text';
				input.value = t;
				if ( cell == 0 ){
					img = document.createElement( 'img' );
					img.src = 'images/table_save.png';
					img.alt = 'save:';
					img.title = 'Save row';
					link = document.createElement( 'a' );
					link.href = '';
					link.appendChild( img );
					link.onclick = ( function ( i, server, db, table ) {
                        return function() {
                            Rows.DoEdit( this.parentNode.parentNode, i, server, db, table );
                            return false;
                        }
					} )( this.oldValues.length - 1 );
					field.appendChild( link );
                    imgCancel = d.createElement( 'img' );
                    imgCancel.src = 'images/cancel.png';
                    imgCancel.alt = 'cancel';
                    imgCancel.title = 'Cancel editing';
                    linkCancel = d.createElement( 'a' );
                    linkCancel.href = '';
                    linkCancel.appendChild( imgCancel );
                    linkCancel.onclick = function() {
                        window.location.reload(); // TODO: do not reload!
                        return false;
                    }
                    field.appendChild( linkCancel );
					field.appendChild( document.createTextNode( ' ' ) );
					firstInput = input;
				}
				field.appendChild( input );
			}
		}
		firstInput.select();
		firstInput.focus();
	},
    DoEdit: function( row, i, server, db, table ) { // TODO: send old values only for primary fields
        var oldvalues = this.oldValues[ i ];
        var newvalues = this.GetRowValues( row );
        Coala.Warm( 'table/row/edit', { "@oldvalues": oldvalues, "@newvalues": newvalues, "row": row } );
    },
    Insert: function( row ) {
        var values = this.GetRowValues( row );
        var metadata = Metadata.GetData();
        Coala.Warm( 'table/insert', { "@values": values, "row": row, "startrow": metadata[ 0 ], "numrows": metadata[ 1 ], "curpage": metadata[ 2 ] } );
    }
};
